package com.kangyi.admin.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.GuestsStayingMapper;
import com.kangyi.admin.service.ToExamineService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.GuestsStayingPageQueryDTO;
import com.kangyi.entity.GuestsStaying;
import com.kangyi.exception.AccountException;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.PageResult;
import com.kangyi.result.ToExaminePageResult;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.NursingHomeBookingPageQueryVO;
import com.kangyi.vo.ToExamineDetailVO;
import com.kangyi.vo.ToExaminePageQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
public class ToExamineServiceImpl implements ToExamineService {

    @Autowired
    private GuestsStayingMapper guestsStayingMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2)  && !adminUserVO.getPermissions().equals(3)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public ToExaminePageResult pageQuery(GuestsStayingPageQueryDTO guestsStayingPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(guestsStayingPageQueryDTO.getPage(),guestsStayingPageQueryDTO.getPageSize());
        Page<ToExaminePageQueryVO> page =  guestsStayingMapper.ToExaminePageQuery(guestsStayingPageQueryDTO);
        long total = page.getTotal();
        List<ToExaminePageQueryVO> result = page.getResult();
        // 初始化不同状态的数量
        int pendingReview = 0;
        int agree = 0;
        int reject = 0;

        // 遍历结果列表，统计不同状态的数量
        for (ToExaminePageQueryVO vo : result) {
            Integer status = vo.getStatus();
            if (status != null){
                if (status == 0){
                    pendingReview++;
                }else if(status == 1){
                    agree++;
                } else if (status == 2) {
                    reject++;
                }
            }
        }

        return new ToExaminePageResult(total,result,pendingReview,agree,reject);
    }

    @Override
    @Transactional
    public void agree(List<Integer> ids) {
        validateUserPermissions();
        for (Integer id : ids) {
            GuestsStaying guestsStaying = guestsStayingMapper.getById(id);
            if (guestsStaying.getStatus().equals(GuestsStaying.APPROVED) || guestsStaying.getStatus().equals(GuestsStaying.REJECTED)) {
                throw new AccountException(MessageConstant.DO_NOT_REPEAT_THE_OPERATION);
            }
            GuestsStaying build = GuestsStaying.builder()
                    .id(id)
                    .status(GuestsStaying.APPROVED)
                    .build();
            guestsStayingMapper.examine(build);
        }
    }

    @Override
    @Transactional
    public void refuse(List<Integer> ids) {
        validateUserPermissions();
        for (Integer id : ids) {
            GuestsStaying guestsStaying = guestsStayingMapper.getById(id);
            if (guestsStaying.getStatus().equals(GuestsStaying.APPROVED) || guestsStaying.getStatus().equals(GuestsStaying.REJECTED)) {
                throw new AccountException(MessageConstant.DO_NOT_REPEAT_THE_OPERATION);
            }
            GuestsStaying build = GuestsStaying.builder()
                    .id(id)
                    .status(GuestsStaying.REJECTED)
                    .build();
            guestsStayingMapper.examine(build);
        }
    }

    @Override
    public ToExamineDetailVO detail(Integer id) {
        validateUserPermissions();
        ToExamineDetailVO toExamineDetailVO = guestsStayingMapper.detail(id);
        if (toExamineDetailVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        return toExamineDetailVO;
    }
}
